package net.zjitc.dao.impl;

import net.zjitc.dao.UserDao;
import net.zjitc.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/27.
 */
@Repository
public class UserDaoImpl implements UserDao {
@Resource
private SessionFactory sessionFactory;

    protected Session getSession(){
        Session session=sessionFactory.getCurrentSession();
        return session;
    }
    @Override
    public User findByUsernamePassword(String username, String password) {

/*        Session session=sessionFactory.openSession();*/

/*        Transaction transaction=session.beginTransaction();*/
        User user=(User)getSession().createQuery(
                "from User  a where a.username=? and a.password=?")
                .setParameter(0,username)
                .setParameter(1,password)
                .uniqueResult();
/*        transaction.commit();*/
/*        session.close();*/
        return user;

    }

    @Override
    public User findByzcUsernamePssword(String username, String password) {
/*        Session session=sessionFactory.openSession();*/
/*        Session session=sessionFactory.getCurrentSession();*/
     /*   Transaction transaction=session.beginTransaction();*/
        User user=(User) getSession().createQuery(
                "from User  a where a.username=? and a.password=?")
                .setParameter(0,username)
                .setParameter(1,password)
                .uniqueResult();
/*        transaction.commit();
        session.close();*/

        return user;
    }

    @Override
    public List<User> findAll() {
        return getSession().createQuery(
                "from User ").list();
    }
}
